package com.seu.multithread.demo02;

/**
 * @author liangfeihu
 * @since 2019/2/18
 */
public class MyThreadPrint {

    static volatile boolean flag = false;
    static volatile int num = 1;

    public static void main(String[] args) throws Exception {

        Thread t1 = new Thread(() -> {
            int times = 10;
            while (times > 0) {
                if (!flag && num % 2 != 0) {
                    System.out.print(num + " ");
                    num++;
                    times--;
                    flag = true;
                }
            }// end while
        });

        Thread t2 = new Thread(() -> {
            int times = 10;
            while (times > 0) {
                if (flag && num % 2 == 0) {
                    System.out.print(num + " ");
                    num++;
                    times--;
                    flag = false;
                }
            }
        });

        t1.start();
        Thread.sleep(100);
        t2.start();

    }

}
